<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 上传文件（基本上都是图片） 操作
 *
 */
class Uploadimglib {
	var $CI;
	function __construct()
	{
		$this->CI = & get_instance();
	}
	
	// 上传图像
	public function upload_image($upload_config=array(), $filed_name='', $apppath='')
	{
		//if(!is_dir($apppath)) $apppath = APPPATH;
		if(!isset($upload_config['upload_path']) || empty($upload_config['upload_path'])) return FALSE;
				
		$path_sections = explode('/', $upload_config['upload_path']);//将路径拆分成数组
		$sections_count = count($path_sections);
		$tmp_path = $apppath . 'images';
		for($i=0; $i<$sections_count; $i++){
			$tmp_path .= '/' . $path_sections[$i];
			//如果目录不存在，则创建它
			if(!is_dir($tmp_path)){
				@mkdir($tmp_path, 0777);
			}
		}
		
		//图片存放的路径默认加上 images，即存放在images目录下
		$upload_config['upload_path'] = $apppath . 'images/' . trim($upload_config['upload_path'], '/') . '/';
		//加载上传类库
		$this->CI->load->library('upload', $upload_config);
		//$this->CI->upload->file_name = 'rrr.jpg';
		if($this->CI->upload->do_upload($filed_name)){
			//上传图片成功
			return $this->CI->upload->upload_path.$this->CI->upload->file_name;//图片存储的路径
		}else{
			return false;
		}
	}
	
	// 重新设置图片大小
	function resize_image($image_config=array(), $path='')
	{
		return $path;
		/*$image_config['source_image'] = $path;//原图
		if(!file_exists($path)) return false;
		
		$this->CI->load->library('image_lib', $image_config);
		if($this->CI->image_lib->resize()){
			return $this->CI->image_lib->full_dst_path;//返回图片路径
		}else{
			return false;
		}*/
	}
	
	/**
	 * 删除图片
	 *
	 * @param unknown_type $path
	 */
	function remove_image($path='', $addapp=1, $apppath='')
	{
		//if(!is_dir($apppath)) $apppath = APPPATH;
		$path = $addapp ? $apppath . $path : $path;
		if(!is_file($path) && !file_exists($path)) return;
		@unlink($path);
	}
	
	/**
	 * 重新设置从数据库中取出的图片的路径
	 *
	 */
	function re_image_path($path='')
	{
		$this->CI->load->helper('url');
		if($path){
			$upload_config = $this->CI->config->item('default_img');
			return site_url() . $upload_config['default_path'] . $path;
		}else{
			return $path;
		}
	}
	
	//处理用户的photo，如果来源合作网站则不需要转换
	function re_user_photo($path='', $reg_type=0)
	{
		if($reg_type == 0){
			//来源本站
			return $this->re_image_path($path);
		}
		return $path;
	}
}